package com.Unidbg_hengdin.System_cm;

import com.alibaba.fastjson.util.IOUtils;
import com.github.unidbg.AndroidEmulator;
import com.github.unidbg.Emulator;
import com.github.unidbg.Module;
import com.github.unidbg.Symbol;
import com.github.unidbg.arm.backend.Unicorn2Factory;
import com.github.unidbg.arm.context.RegisterContext;
import com.github.unidbg.debugger.BreakPointCallback;
import com.github.unidbg.linux.android.AndroidEmulatorBuilder;
import com.github.unidbg.linux.android.AndroidResolver;
import com.github.unidbg.linux.android.SystemPropertyHook;
import com.github.unidbg.linux.android.SystemPropertyProvider;
import com.github.unidbg.linux.android.dvm.AbstractJni;
import com.github.unidbg.linux.android.dvm.DalvikModule;
import com.github.unidbg.linux.android.dvm.DvmClass;
import com.github.unidbg.linux.android.dvm.VM;
import com.github.unidbg.memory.Memory;

import java.io.File;

public class Study10_02 extends AbstractJni {
    private final AndroidEmulator emulator;
    private final VM vm;

    private final Memory memory;

    private final Module module;

    private final boolean logging;

    private final DvmClass NativeHelperUtils;

    private final  String  hengdin = "hengdin";

    Study10_02(boolean logging ){

        this.logging = logging;
        emulator = AndroidEmulatorBuilder.for64Bit()
                .setProcessName("com.xiaojianbang.app")
                .addBackendFactory(new Unicorn2Factory(true))
                .build();

        memory = emulator.getMemory();
        memory.setLibraryResolver(new AndroidResolver(23));
        //  -------------------------  systemPropertyHook ---------------------
        SystemPropertyHook systemPropertyHook = new SystemPropertyHook(emulator);
        systemPropertyHook.setPropertyProvider(new SystemPropertyProvider() {
            @Override
            public String getProperty(String key) {

                String res = this.getProperty(key);
                System.out.println(" systemProperty hooked key: " + key + ": value: " + res);
                System.out.println(" systemProperty hooked key: " + key );
                return null;
            }
        });
        memory.addHookListener(systemPropertyHook);
        //  -------------------------  systemPropertyHook ---------------------



        vm = emulator.createDalvikVM();
        vm.setVerbose(logging);
        vm.setJni(this);

        DalvikModule dalvikModule = vm.loadLibrary(new File("unidbg-android/src/test/java/com/Unidbg_hengdin/System_cm/libxiaojianbang.so"), false);
        module = dalvikModule.getModule();


        NativeHelperUtils = vm.resolveClass("com/xiaojianbang/app/NativeHelper");


    }

    void destory(){
        IOUtils.close(emulator);
        if(logging){
            System.out.println("destory !");
        }
    }
    public static void main(String[] args){
        Study10_02 test = new Study10_02(true);
        test.callFun();
        test.destory();
    }
    void callFun(){

//        env3();
//
//        env1();
//        env2();
//
//        env4();

    }

    public void hook_init(){
        emulator.attach().addBreakPoint(module.findSymbolByName("popen").getAddress(), new BreakPointCallback() {
            @Override
            public boolean onHit(Emulator<?> emulator, long address) {
                RegisterContext context = emulator.getContext();
                String commad = context.getPointerArg(0).getString(0);
                System.out.println("popen args " + commad);
                return true;
            }
        });


    }
    public void env2(){
        String res = emulator.get(hengdin);
        System.out.println("hengdin res : " + res);

        String pathRes = emulator.get("PATH");
        System.out.println("hengdin res : " + pathRes);

        String xxxRes = emulator.get("PATH");
        System.out.println("hengdin xxxRes res : " + xxxRes);
    }




}
